package cn.exrick.xboot.flow.controller;


import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.config.custom.CustomPageUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.exrick.xboot.flow.entity.FlowTaskInstance;
import cn.exrick.xboot.flow.service.IFlowTaskInstanceService;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * @author zy
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/order/flowTaskInstance")
public class FlowTaskInstanceController {
    private final Logger logger = LoggerFactory.getLogger(FlowTaskInstanceController.class);

    @Autowired
    public IFlowTaskInstanceService iFlowTaskInstanceService;

    /**
     * 分页查询数据
     *
     * @param page             分页信息
     * @param flowTaskInstance 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getFlowTaskInstancePageList")
    public Result<Object> getHontUserList(Page<FlowTaskInstance> page, FlowTaskInstance flowTaskInstance) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iFlowTaskInstanceService.selectPage(page, flowTaskInstance)));
    }

    /**
     * 保存和修改公用的
     *
     * @param flowTaskInstance 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/flowTaskInstanceSave")
    public Result<Object> flowTaskInstanceSave(FlowTaskInstance flowTaskInstance) {
        iFlowTaskInstanceService.saveOrUpdate(flowTaskInstance);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/flowTaskInstanceGetById")
    public Result<Object> flowTaskInstanceGetById(String id) {
        FlowTaskInstance byId = iFlowTaskInstanceService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/flowTaskInstanceDelete")
    public Result<Object> hontUserDelete(String id) {
        iFlowTaskInstanceService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/flowTaskInstanceBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iFlowTaskInstanceService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }
    /**
     * 功能描述: <br>
     * 〈〉审批
     * @Param: [flowInstanceId, opinion]
     * @Return: com.hkts.workflow.utils.Result<java.lang.Object>
     * @Author: zhangyong
     * @Date: 2022/9/6 上午9:54
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/approval")
    public Result<Object> approval(String flowInstanceId,String opinion) {

        return iFlowTaskInstanceService.approval(flowInstanceId,opinion);
    }
}
